<!DOCTYPE html>
<html>
<head>
<title>Physics Diagram of a Sphere in a Dielectric Medium</title>
</head>
<body>

<canvas id="physicsCanvas" width="450" height="400"></canvas>

<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    const centerX = 220;
    const centerY = 180;
    const radius = 85;

    // Helper function to draw an arrow with a filled head
    function drawFilledArrow(ctx, fromx, fromy, tox, toy, lineWidth) {
        const headlen = 12; // length of head in pixels
        const angle = Math.atan2(toy - fromy, tox - fromx);

        ctx.save();
        ctx.strokeStyle = 'black';
        ctx.lineWidth = lineWidth;

        // Main line of the arrow
        ctx.beginPath();
        ctx.moveTo(fromx, fromy);
        ctx.lineTo(tox, toy);
        ctx.stroke();

        // Arrow head
        ctx.beginPath();
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 7), toy - headlen * Math.sin(angle - Math.PI / 7));
        ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 7), toy - headlen * Math.sin(angle + Math.PI / 7));
        ctx.closePath();
        ctx.fillStyle = 'black';
        ctx.fill();

        ctx.restore();
    }

    // --- Drawing Start ---

    // 1. Draw horizontal lines for the uniform dielectric medium
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1.5;
    const lineSpacing = 18;
    for (let y = lineSpacing; y < canvas.height - 50; y += lineSpacing) {
        // Check if the line intersects the sphere
        const dyAbs = Math.abs(y - centerY);
        if (dyAbs < radius) {
            // Line intersects, so it's broken by the sphere. Draw two segments.
            const dx = Math.sqrt(radius * radius - dyAbs * dyAbs);
            const x1 = centerX - dx;
            const x2 = centerX + dx;

            // Draw left segment
            ctx.beginPath();
            ctx.moveTo(0, y);
            ctx.lineTo(x1, y);
            ctx.stroke();

            // Draw right segment
            ctx.beginPath();
            ctx.moveTo(x2, y);
            ctx.lineTo(canvas.width, y);
            ctx.stroke();
        } else {
            // Line is outside the sphere, draw a full line
            ctx.beginPath();
            ctx.moveTo(0, y);
            ctx.lineTo(canvas.width, y);
            ctx.stroke();
        }
    }
     // Draw the few broken lines at the bottom as in the original image
    const bottomY1 = centerY + radius + 15;
    const bottomY2 = bottomY1 + lineSpacing;
    const segmentLength = 100;
    ctx.beginPath();
    ctx.moveTo(centerX - segmentLength/2, bottomY1);
    ctx.lineTo(centerX + segmentLength/2, bottomY1);
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(centerX - segmentLength/2, bottomY2);
    ctx.lineTo(centerX + segmentLength/2, bottomY2);
    ctx.stroke();


    // 2. Draw the sphere
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
    ctx.fillStyle = 'white'; // Fill with white to obscure the lines behind it
    ctx.fill();
    ctx.lineWidth = 3;
    ctx.strokeStyle = 'black';
    ctx.stroke();

    // 3. Draw radii defining the sector for label 'a'
    ctx.lineWidth = 2;
    const angle_a1 = 0; // 0 degrees (horizontal to the right)
    const angle_a2 = -Math.PI / 5; // -36 degrees (up and to the right)
    
    // Draw radius 1
    ctx.beginPath();
    ctx.moveTo(centerX, centerY);
    ctx.lineTo(centerX + radius * Math.cos(angle_a1), centerY + radius * Math.sin(angle_a1));
    ctx.stroke();

    // Draw radius 2
    ctx.beginPath();
    ctx.moveTo(centerX, centerY);
    ctx.lineTo(centerX + radius * Math.cos(angle_a2), centerY + radius * Math.sin(angle_a2));
    ctx.stroke();

    // 4. Draw the position vector 'r'
    const angle_r = 3 * Math.PI / 4; // 135 degrees
    const r_length = radius + 40;
    const r_endX = centerX + r_length * Math.cos(angle_r);
    const r_endY = centerY + r_length * Math.sin(angle_r);
    drawFilledArrow(ctx, centerX, centerY, r_endX, r_endY, 2.5);

    // 5. Add all labels and text
    ctx.fillStyle = 'black';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Label 'a' for radius
    ctx.font = 'italic 22px Times New Roman';
    const angle_a_mid = (angle_a1 + angle_a2) / 2;
    const r_a_label_pos = radius * 0.6;
    ctx.fillText('a', centerX + r_a_label_pos * Math.cos(angle_a_mid), centerY + r_a_label_pos * Math.sin(angle_a_mid));

    // Label 'r' for the position vector
    ctx.font = 'bold italic 22px Times New Roman';
    ctx.fillText('r', r_endX - 10, r_endY - 18);

    // Label 'ε' for the dielectric constant
    ctx.font = '24px Times New Roman';
    ctx.fillText('ε', centerX + radius + 30, centerY + 10);
    
    // 6. Add the figure caption
    ctx.font = 'bold 20px Times New Roman';
    ctx.fillText('Fig. 1.52', centerX, canvas.height - 25);

</script>

</body>
</html>